package com.community.web.user.controller.orders;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.common.login.LoginUserHolder;
import com.community.common.result.Result;
import com.community.model.entity.OrderDetail;
import com.community.model.entity.Orders;
import com.community.web.user.service.OrderDetailService;
import com.community.web.user.service.OrdersService;
import com.community.web.user.vo.OrdersQueryVo;
import com.community.web.user.vo.OrdersVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "订单模块")
@RestController
@RequestMapping("/user/orders")
public class OrdersController {
    @Autowired
    private OrdersService service;

    @ApiOperation("提交订单")
    @PostMapping()
    public Result<Orders> add(@RequestBody List<Long> cartIdList){
        Long userId = LoginUserHolder.getLoginUser().getUserId();
        Orders orders = service.add(userId, cartIdList);
        return Result.ok(orders);
    }

    @ApiOperation("订单支付")
    @PutMapping()
    public Result payment(@RequestBody Orders orders){
        Long userId = LoginUserHolder.getLoginUser().getUserId();
        service.payment(userId, orders);
        return Result.ok();
    }

    @ApiOperation("根据条件分页查询订单列表")
    @GetMapping("{current}/{size}")
    public Result<IPage<OrdersVo>> selectList(@PathVariable long current, @PathVariable long size, OrdersQueryVo queryVo){
        Page<OrdersVo> page = new Page<>(current, size);
        IPage<OrdersVo> list = service.pageList(page, queryVo);
        return Result.ok(list);
    }

    @ApiOperation("根据订单编号查询订单详情")
    @GetMapping("{ordersNo}")
    public Result<OrdersVo> selectDetailList(@PathVariable String ordersNo){
        OrdersVo result = service.selectDetailList(ordersNo);
        return Result.ok(result);
    }


}
